﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppEnvironment.Lib;
using System.Configuration;

namespace AppEnvironment
{
    public static class Logger
    {
        public static void Info(string message)
        {
            LoggerProvider provider = App.New<LoggerProvider>();
            provider.Info(GetSourceName(), GetLocation(), message);
        }
        
        public static void Error(string message)
        {
            LoggerProvider provider = App.New<LoggerProvider>();
            provider.Error(GetSourceName(), GetLocation(), message);
        }

        public static void Error(Exception exception)
        {
            LoggerProvider provider = App.New<LoggerProvider>();
            provider.Error(GetSourceName(), GetLocation(), exception);
        }

        private static string GetLocation()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }
        
        private static string GetSourceName()
        {
            string name = ConfigurationManager.AppSettings["ApplicationName"];
            if (string.IsNullOrEmpty(name))
            {
                if (AppDomain.CurrentDomain != null)
                {
                    if (!string.IsNullOrEmpty(AppDomain.CurrentDomain.BaseDirectory))
                    {
                        name = AppDomain.CurrentDomain.BaseDirectory;
                    }
                }
                if (string.IsNullOrEmpty(name))
                {
                    System.Reflection.Assembly caller = System.Reflection.Assembly.GetEntryAssembly();
                    if (caller != null)
                    {
                        System.Reflection.AssemblyName assemblyName = caller.GetName(false);
                        if (assemblyName != null)
                        {
                            name = assemblyName.Name;
                        }
                    }
                    if (string.IsNullOrEmpty(name))
                    {
                        name = System.Reflection.Assembly.GetExecutingAssembly().FullName;
                    }
                }
            }
            return name;
        }

    }
}
